// pages/view-invoice/view-invoice.js
Page({
  data: {
    invoiceInfo: {},
    invoiceDetails: {
      invoiceNumber: '',
      issueDate: '',
      status: '已开具',
      downloadUrl: '',
      previewUrl: ''
    }
  },

  onLoad: function(options) {
    // 接收发票信息
    this.setData({
      invoiceInfo: {
        id: options.invoiceId,
        type: options.type,
        amount: options.amount,
        date: options.date,
        department: options.department,
        doctor: options.doctor
      }
    });

    // 生成发票详情
    this.generateInvoiceDetails();
  },

  // 生成发票详情
  generateInvoiceDetails: function() {
    const { invoiceInfo } = this.data;
    const today = new Date();
    const issueDate = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
    
    this.setData({
      invoiceDetails: {
        invoiceNumber: `INV${invoiceInfo.id}${Date.now().toString().slice(-6)}`,
        issueDate: issueDate,
        status: '已开具',
        downloadUrl: '#',
        previewUrl: '#'
      }
    });
  },

  // 预览发票
  previewInvoice: function() {
    wx.navigateTo({
      url: `/pages/invoice-preview/invoice-preview?invoiceId=${this.data.invoiceInfo.id}`
    });
  },

  // 返回首页
  goHome: function() {
    wx.reLaunch({
      url: '/pages/index/index'
    });
  }
});

